﻿
using DABLL;
using DADAL;
using Microsoft.IdentityModel.Tokens;
using NetTaste;
using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class TemplateAddForm : BaseForm
    {
        #region 属性
        #endregion

        #region 自定义方法
        #endregion

        #region 系统方法

        public TemplateAddForm()
        {
            InitializeComponent();
            Header.Text = "试压模板添加";
        }

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            TemplateModel model = new TemplateModel();
            if (NumberInput.Text.IsNullOrEmpty())
            {
                Error("请输入模板编号");
                return;
            }
            model.Number = NumberInput.Text;
            if (PipePositionSelect.Text.IsNullOrEmpty())
            {
                Error("请选择管道位置");
                return;
            }
            model.PipePosition = PipePositionSelect.Text;
            model.ErrorRange = Convert.ToInt32(ErrorRangeInput.Text);

            try
            {
                model.DesignPressure = Math.Round(double.Parse(DesignPressureInput.Text), 3);
            }
            catch (Exception)
            {
                Error("请输入正确的压力值");
                return;
            }
            int stageCount = 0;
            if (!FirstStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.FirstStagePressure = Math.Round(double.Parse(FirstStagePressureInput.Text), 3);
                    if (!FirstStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.FirstStageTime = int.Parse(FirstStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段一持续时间");
                        return;
                    }

                    if (!FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.FirstStageTimeUnit = FirstStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段一时间单位");
                        return;
                    }

                    model.FirstStageEstimateWay = FirstStageEstimateWaySelect.Text;

                    if (!FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.FirstStageEstimateValue = Math.Round(double.Parse(FirstStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段一判断数值");
                        return;
                    }
                    stageCount++;
                }
                catch (Exception)
                {
                    Error("请输入阶段一持续时间");
                    return;
                }
            }

            if (!SecondStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.SecondStagePressure = Math.Round(double.Parse(SecondStagePressureInput.Text), 3);
                    if (!SecondStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.SecondStageTime = int.Parse(SecondStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段二持续时间");
                        return;
                    }

                    if (!SecondStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.SecondStageTimeUnit = SecondStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段二时间单位");
                        return;
                    }

                    model.SecondStageEstimateWay = SecondStageEstimateWaySelect.Text;

                    if (!SecondStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.SecondStageEstimateValue = Math.Round(double.Parse(SecondStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段二判断数值");
                        return;
                    }
                    stageCount++;
                }
                catch (Exception)
                {
                    Error("请输入阶段二持续时间");
                    return;
                }
            }

            if (!ThirdStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.ThirdStagePressure = Math.Round(double.Parse(ThirdStagePressureInput.Text), 3);
                    if (!ThirdStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageTime = int.Parse(ThirdStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段三持续时间");
                        return;
                    }

                    if (!ThirdStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageTimeUnit = ThirdStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段三时间单位");
                        return;
                    }

                    model.ThirdStageEstimateWay = ThirdStageEstimateWaySelect.Text;

                    if (!ThirdStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageEstimateValue = Math.Round(double.Parse(ThirdStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段三判断数值");
                        return;
                    }
                    stageCount++;
                }
                catch (Exception)
                {
                    Error("请输入阶段三持续时间");
                    return;
                }
            }

            if (!IntensityStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.IntensityStagePressure = Math.Round(double.Parse(IntensityStagePressureInput.Text), 3);
                    if (!IntensityStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageTime = int.Parse(IntensityStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入强度段持续时间");
                        return;
                    }

                    if (!IntensityStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageTimeUnit = IntensityStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择强度段时间单位");
                        return;
                    }

                    model.IntensityStageEstimateWay = IntensityStageEstimateWaySelect.Text;

                    if (!IntensityStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageEstimateValue = Math.Round(double.Parse(IntensityStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入强度段判断数值");
                        return;
                    }
                    stageCount++;
                }
                catch (Exception)
                {
                    Error("请输入强度段持续时间");
                    return;
                }
            }

            if (!RigorStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.RigorStagePressure = Math.Round(double.Parse(RigorStagePressureInput.Text), 3);
                    if (!RigorStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.RigorStageTime = int.Parse(RigorStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入严密性持续时间");
                        return;
                    }

                    if (!RigorStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.RigorStageTimeUnit = RigorStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择严密性时间单位");
                        return;
                    }

                    model.RigorStageEstimateWay = RigorStageEstimateWaySelect.Text;

                    if (!RigorStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.RigorStageEstimateValue = Math.Round(double.Parse(RigorStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入严密性判断数值");
                        return;
                    }
                    stageCount++;
                }
                catch (Exception)
                {
                    Error("请输入严密性持续时间");
                    return;
                }
            }
            if (stageCount == 0)
            {
                Error( "当前模板无试压阶段，不能添加");
                return;
            }
            model.StageCount = stageCount;

            Response response = Template.AddTemplate(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                this.Close();
                if (this.Owner is not null)
                {
                    TemplateManageForm frm = (TemplateManageForm)this.Owner;
                    frm.LoadList();
                }
            }
        }

        #endregion

    }
}
